package com.dhcc.DRGsHosp.facade.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Named;

import org.openkoala.koala.commons.InvokeResult;
import org.springframework.beans.factory.annotation.Autowired;

import com.dhcc.DRGsHosp.application.DepGroupApplication;
import com.dhcc.DRGsHosp.application.DepartmentApplication;
import com.dhcc.DRGsHosp.application.DictionaryDataApplication;
import com.dhcc.DRGsHosp.application.MedicalRecordApplication;
import com.dhcc.DRGsHosp.application.MonitorResultApplication;
import com.dhcc.DRGsHosp.application.YiZhengHomeApplication;
import com.dhcc.DRGsHosp.facade.DrgsIndexCalFacade;
import com.dhcc.DRGsHosp.facade.YiZhengHomeFacade;
import com.dhcc.DRGsHosp.facade.dto.IndexCalDTO;
import com.dhcc.DRGsHosp.facade.impl.assembler.IndexCalAssembler;
import com.dhcc.DRGsHosp.infra.util.GlobalData;

/**
 * @title		:YiZhengHomeFacadeImpl.java
 * @description	:
 * @author		:liqingyang
 * @date		:2015年4月29日
 * @version     :1.0
 * @copyright   :DHCC 2013-2014 
 */
@Named("drgsHospFacadeImpl49")
public class YiZhengHomeFacadeImpl implements YiZhengHomeFacade{
	

	@Autowired
	private MonitorResultApplication application;
	
	@Autowired 
	private DepGroupApplication depGroupApplication;
	
	@Autowired
	private DepartmentApplication departmentApplication;
	
	@Autowired
	private MedicalRecordApplication medicalRecordApplication;
	
	@Autowired 
	private DictionaryDataApplication dictionaryDataApplication;
	
	@Autowired 
	YiZhengHomeApplication yiZhengHomeApplication;
	
	@Autowired
	private DrgsIndexCalFacade drgsIndexCalFacade;
		@Override
		public InvokeResult getLatestDate() {
			return InvokeResult.success(medicalRecordApplication.getLatestDate());
		}
 
		@Override
		public InvokeResult getTotalAnalysis(String s, String e) {
			// 获取标杆定位id 
		    String stdId = drgsIndexCalFacade.getStdID(s, e);
			return InvokeResult.success(yiZhengHomeApplication.getTotalAnalysis(s, e,stdId));
		}
		
		@Override
		public InvokeResult getDisCaseInfo(String s, String e) {
			return InvokeResult.success(yiZhengHomeApplication.getDisCaseInfo(s, e));
		}

		@Override
		public InvokeResult getDrgDataInMdc(String s, String e,String mdcCode) {
			return InvokeResult.success(yiZhengHomeApplication.getDrgDataInMdc(s, e,mdcCode));
		}

		@Override
		public InvokeResult getOtherCase(String s, String e) {
			return InvokeResult.success(yiZhengHomeApplication.getOtherCase(s, e));
		}

		@Override
		public InvokeResult getDepCaseData(String s, String e) {
			return InvokeResult.success(yiZhengHomeApplication.getDepCaseData(s, e));
		}
		/**
		 * 
		 *  @Description：获取病案首页基本信息
		 *  @Author LiuHongJun  
		 *  @see com.dhcc.DRGsHosp.facade.YiZhengHomeFacade#getPatientInfo(java.lang.String)
		 */
		@Override
		public InvokeResult getPatientInfo(String patientId,String patientversion) {
			List<Object[]> operate = new ArrayList<>();
			List<Object[]> otherOperate = new ArrayList<>();
			//operate=yiZhengHomeApplication.getOperate(patientId);
			Map<String,Object> map = new HashMap<String, Object>();
			Map<String,Object> costMap = new HashMap<String,Object>();
			Map<String,Object> patient = yiZhengHomeApplication.getPatientInfo(patientId);//基本信息
			// dic_code#dic_value : dic_desc  键值对
			List<Map<String, Object>> dictionaryDataList = dictionaryDataApplication.findDictionaryDataByCode("");
			Map<String,Object> dictionaryDataMap = new HashMap<String,Object>();
			for(Map<String, Object> mapo:dictionaryDataList){
				String code=mapo.get("DICCODE")==null?"":mapo.get("DICCODE").toString();
				String desc=mapo.get("DICDESC")==null?"":mapo.get("DICDESC").toString();
				String value=mapo.get("DICVALUE")==null?"":mapo.get("DICVALUE").toString();
				dictionaryDataMap.put(code+"#"+value, desc);
			}
			//针对 patient中的每个键 做映射
			dictionaryValuedMapping(dictionaryDataMap,patient) ;
			Map<String,Object> operation = yiZhengHomeApplication.getOperation(patientId,patientversion);//主要手术信息
			dictionaryValuedMapping(dictionaryDataMap,operation) ;
			// 其他手术信息
			List<Map<String,Object>> otherOperationTemp = yiZhengHomeApplication.getOtherOperation(patientId,patientversion);//其他手术信息
			List<Map<String,Object>> otherOperation = new ArrayList<>() ;
			for(Map<String,Object> mapOtherOperation:otherOperationTemp) {
				dictionaryValuedMapping(dictionaryDataMap,mapOtherOperation) ;
				otherOperation.add(mapOtherOperation) ;
			}
			
			if(patientversion.equals("BJ")){
				operate=yiZhengHomeApplication.getOperate(patientId);//主要操作信息
				otherOperate = yiZhengHomeApplication.getOtherOperate(patientId);//其他操作信息	
			}
			
			List<Object[]> maindiagnosis = yiZhengHomeApplication.getmainDiagnosis(patientId,patientversion);//主要诊断信息
			List<Object[]> diagnosis = yiZhengHomeApplication.getDiagnosis(patientId,patientversion);//其他诊断信息
			
			List<Object[]> cost = yiZhengHomeApplication.getCost(patientId);
			//获取费用大类
			List<Object[]> costType = yiZhengHomeApplication.getCostType();
			for(int i = 0;i<cost.size();i++)
			{
				costMap.put(cost.get(i)[2].toString(),cost.get(i));
			}
			//山西病案首页附页信息
			Map<String,Object> appendpage = new HashMap<String, Object>();
			String hospName=GlobalData.hospName==null?"":GlobalData.hospName+"";
			if(patientversion.equals("CN")&&hospName.contains("中铁十七局")){
				 appendpage = yiZhengHomeApplication.getAppendpageInfo(patientId);
				 map.put("appendpage", appendpage);
			}
			map.put("patient", patient);
			map.put("maindiagnosis",maindiagnosis);
			map.put("diagnosis", diagnosis);
			map.put("operation", operation);
			map.put("otherOperation", otherOperation);
			if(patientversion.equals("BJ")){
				map.put("operate", operate);
				map.put("otherOperate", otherOperate);	
			}
			map.put("cost", cost);
			map.put("costMap", costMap);
			map.put("costType", costType);
			return InvokeResult.success(map);
		}

		
		@Override
		public InvokeResult getCaseQYData(String startDate, String endDate,
				String notincode) {
			return InvokeResult.success(yiZhengHomeApplication.getCaseQYData(startDate,endDate,notincode));
		}

		@Override
		public InvokeResult getPatientCost(String patientId) {
			Map<String, Object> result = new HashMap<>();
			List<Object[]> costs = yiZhengHomeApplication.getPatientCost(patientId);
			Map<String, Object> costMap = new HashMap<>();
			for(Object[] objs: costs){
				costMap.put("cost"+objs[0].toString(), objs[1]);
			}
			double fee1 = (costMap.get("cost1")!=null?Double.parseDouble(costMap.get("cost1").toString()):0) + (costMap.get("cost26")!=null?Double.parseDouble(costMap.get("cost26").toString()):0) + (costMap.get("cost27")!=null?Double.parseDouble(costMap.get("cost27").toString()):0); //1 - 一般医疗服务费
			result.put("fee1", fee1);
			double fee2 = (costMap.get("cost8")!=null?Double.parseDouble(costMap.get("cost8").toString()):0) + (costMap.get("cost21")!=null?Double.parseDouble(costMap.get("cost21").toString()):0) + (costMap.get("cost13")!=null?Double.parseDouble(costMap.get("cost13").toString()):0) + (costMap.get("cost29")!=null?Double.parseDouble(costMap.get("cost29").toString()):0); //2 - 一般治疗操作费
			result.put("fee2", fee2);
			double fee3 = (costMap.get("cost14")!=null?Double.parseDouble(costMap.get("cost14").toString()):0); //3 - 护理费	
			result.put("fee3", fee3);
			double fee4 = (costMap.get("cost38")!=null?Double.parseDouble(costMap.get("cost38").toString()):0); //4 - 其他费用、
			result.put("fee4", fee4);
			double fee5 = (costMap.get("cost20")!=null?Double.parseDouble(costMap.get("cost20").toString()):0); //5 - 病理诊断费
			result.put("fee5", fee5);
			double fee6 = (costMap.get("cost19")!=null?Double.parseDouble(costMap.get("cost19").toString()):0); //6 - 实验室诊断费
			result.put("fee6", fee6);
			double fee7 = (costMap.get("cost15")!=null?Double.parseDouble(costMap.get("cost15").toString()):0) + (costMap.get("cost17")!=null?Double.parseDouble(costMap.get("cost17").toString()):0) + (costMap.get("cost18")!=null?Double.parseDouble(costMap.get("cost18").toString()):0); //7 - 影像学诊断费
			result.put("fee7", fee7);
			double fee8 = (costMap.get("cost2")!=null?Double.parseDouble(costMap.get("cost2").toString()):0); //8 - 临床诊断项目费
			result.put("fee8", fee8);
			double fee9 = (costMap.get("cost3")!=null?Double.parseDouble(costMap.get("cost3").toString()):0) + (costMap.get("cost16")!=null?Double.parseDouble(costMap.get("cost16").toString()):0) + (costMap.get("cost5")!=null?Double.parseDouble(costMap.get("cost5").toString()):0) + (costMap.get("cost9")!=null?Double.parseDouble(costMap.get("cost9").toString()):0); //9 - 非手术治疗项目费
			result.put("fee9", fee9);
			double fee9_1 = (costMap.get("cost3")!=null?Double.parseDouble(costMap.get("cost3").toString()):0) + (costMap.get("cost16")!=null?Double.parseDouble(costMap.get("cost16").toString()):0); //9.1 - 临床物理治疗费
			result.put("fee9_1", fee9_1);
			double fee10 = (costMap.get("cost11")!=null?Double.parseDouble(costMap.get("cost11").toString()):0) + (costMap.get("cost12")!=null?Double.parseDouble(costMap.get("cost12").toString()):0) + (costMap.get("cost4")!=null?Double.parseDouble(costMap.get("cost4").toString()):0) + (costMap.get("cost10")!=null?Double.parseDouble(costMap.get("cost10").toString()):0); //10 - 手术治疗费
			result.put("fee10", fee10);
			double fee10_1 = (costMap.get("cost11")!=null?Double.parseDouble(costMap.get("cost11").toString()):0); //10.1 - 麻醉费
			result.put("fee10_1", fee10_1);
			double fee10_2 = (costMap.get("cost12")!=null?Double.parseDouble(costMap.get("cost12").toString()):0); //10.2 - 手术费
			result.put("fee10_2", fee10_2);
			double fee11 = (costMap.get("cost6")!=null?Double.parseDouble(costMap.get("cost6").toString()):0); //11 - 康复费
			result.put("fee11", fee11);
			double fee12 = (costMap.get("cost7")!=null?Double.parseDouble(costMap.get("cost7").toString()):0); //12 - 中医治疗费 
			result.put("fee12", fee12);
			double fee13 = (costMap.get("cost30")!=null?Double.parseDouble(costMap.get("cost30").toString()):0) + (costMap.get("cost31")!=null?Double.parseDouble(costMap.get("cost31").toString()):0); //13 - 西药费 
			result.put("fee13", fee13);
			double fee13_1 = (costMap.get("cost31")!=null?Double.parseDouble(costMap.get("cost31").toString()):0); //13.1 - 抗菌药物费用
			result.put("fee13_1", fee13_1);
			double fee14 = (costMap.get("cost36")!=null?Double.parseDouble(costMap.get("cost36").toString()):0); //14 - 中成药费
			result.put("fee14", fee14);
			double fee15 = (costMap.get("cost37")!=null?Double.parseDouble(costMap.get("cost37").toString()):0); //15 - 中草药费
			result.put("fee15", fee15);
			double fee16 = (costMap.get("cost29")!=null?Double.parseDouble(costMap.get("cost29").toString()):0); //16 - 血费
			result.put("fee16", fee16);
			double fee17 = (costMap.get("cost32")!=null?Double.parseDouble(costMap.get("cost32").toString()):0); //17 - 白蛋白类制品费
			result.put("fee17", fee17);
			double fee18 = (costMap.get("cost33")!=null?Double.parseDouble(costMap.get("cost33").toString()):0); //18 - 球蛋白类制品费
			result.put("fee18", fee18);
			double fee19 = (costMap.get("cost34")!=null?Double.parseDouble(costMap.get("cost34").toString()):0); //19 - 凝血因子类制品费
			result.put("fee19", fee19);
			double fee20 = (costMap.get("cost35")!=null?Double.parseDouble(costMap.get("cost35").toString()):0); //20 - 细胞因子类制品费
			result.put("fee20", fee20);
			double fee21 = (costMap.get("cost25")!=null?Double.parseDouble(costMap.get("cost25").toString()):0); //21 - 检查用一次性医用材料费
			result.put("fee21", fee21);
			double fee22 = (costMap.get("cost22")!=null?Double.parseDouble(costMap.get("cost22").toString()):0); //22 - 治疗用一次性医用材料费
			result.put("fee22", fee22);
			double fee23 = (costMap.get("cost23")!=null?Double.parseDouble(costMap.get("cost23").toString()):0) + (costMap.get("cost24")!=null?Double.parseDouble(costMap.get("cost24").toString()):0); //23 - 手术用一次性医用材料费
			result.put("fee23", fee23);
			double fee24 = 0; //20 - 细胞因子类制品费
			result.put("fee24", fee24);
			return InvokeResult.success(result);
		}
		/**
		 * 
		 *  @Description：获取科室信息
		 *  @Author LiuHongJun  
		 *  @see com.dhcc.DRGsHosp.facade.YiZhengHomeFacade#getDepInDG(com.dhcc.DRGsHosp.facade.dto.IndexCalDTO)
		 */
		@Override
		public InvokeResult getDepInDG(IndexCalDTO dto) {
			List<Map<String,Object>> listMap = departmentApplication.findDepsByDepGroup(IndexCalAssembler.toEntity(dto));
			return InvokeResult.success(listMap);
		}
		/**
		 * 
		 *  @Description：获取医师
		 *  @Author LiuHongJun  
		 *  @see com.dhcc.DRGsHosp.facade.YiZhengHomeFacade#getProtomedicusByDepCode(com.dhcc.DRGsHosp.facade.dto.IndexCalDTO)
		 */
		@Override
		public InvokeResult getProtomedicusByDepCode(IndexCalDTO dto){
			return InvokeResult.success( application.getProtomedicusByDepCode(IndexCalAssembler.toEntity(dto)));
		}
/**
 * 
 *  @Description：获取主诊组
 *  @Author LiuHongJun  
 *  @see com.dhcc.DRGsHosp.facade.YiZhengHomeFacade#getClinicInDep(com.dhcc.DRGsHosp.facade.dto.IndexCalDTO)
 */
		@Override
		public InvokeResult getClinicInDep(IndexCalDTO dto) {
			return InvokeResult.success(application.getClinicByDepCode(IndexCalAssembler.toEntity(dto)));
		}
		
		/**
		 * 
		 *  @Description：获取科室组信息
		 *  @Author LiuHongJun  
		 *  @see com.dhcc.DRGsHosp.facade.YiZhengHomeFacade#findAllDepGroupInCon(com.dhcc.DRGsHosp.facade.dto.IndexCalDTO)
		 */
		@Override
		public InvokeResult findAllDepGroupInCon(IndexCalDTO dto) {
			List<Map<String,Object>> listMap= depGroupApplication.findDepGroups(IndexCalAssembler.toEntity(dto));
			return InvokeResult.success(listMap);
		}

		@Override
		public InvokeResult getCostnameInfo(String costsubcode) {
			Map<String,Object> map = new HashMap<String, Object>();
			//获取费用大类
			List<Object[]> costname = yiZhengHomeApplication.getCostnameInfo(costsubcode);			
			map.put("costname", costname);
			return InvokeResult.success(map);
		}
/**
 * 
 *  @Description：获取科室下的病区
 *  @Author LiuHongJun  
 *  @see com.dhcc.DRGsHosp.facade.YiZhengHomeFacade#getWardInPerf(com.dhcc.DRGsHosp.facade.dto.IndexCalDTO)
 */
		@Override
		public InvokeResult getWardInPerf(IndexCalDTO dto) {
			return InvokeResult.success(application.findWardsByCondition(IndexCalAssembler.toEntity(dto)));
		}
		/**
		 * 
		 *  @Description： 获取当前科室下的所有住院医师
		 *  @Author LiuHongJun  
		 *  @see com.dhcc.DRGsHosp.facade.YiZhengHomeFacade#getResidentInDep(com.dhcc.DRGsHosp.facade.dto.IndexCalDTO)
		 */
		@Override
		public InvokeResult getResidentInDep(IndexCalDTO dto) {
			return InvokeResult.success(application.getResidentByDepCode(IndexCalAssembler.toEntity(dto)));
		}
/**
 * 
 *  @Description：获取医师
 *  @Author LiuHongJun  
 *  @see com.dhcc.DRGsHosp.facade.YiZhengHomeFacade#getPhysicianInDep(com.dhcc.DRGsHosp.facade.dto.IndexCalDTO)
 */
		@Override
		public InvokeResult getPhysicianInDep(IndexCalDTO dto) {
			return InvokeResult.success(application.getPhysicianByDepCode(IndexCalAssembler.toEntity(dto)));
		}
		
		/**
		 *  对象的属性值 增加字典值描述
		 *  dictionaryValuedMapping.  
		 *  @Description：
		 *  @Author ChenHanlin
		 *  @param dictionaryDataMap 字典值
		 *  @param mapObject  需要映射的对象
		 *  @Date:2017年9月21日上午10:46:13
		 */
		private void dictionaryValuedMapping(Map<String,Object> dictionaryDataMap,Map<String,Object> mapObject) {
			if(mapObject == null) {
				return ;
			}
			for (Map.Entry<String, Object> entry : mapObject.entrySet()) {
				String desc = null ;
				Object obj = dictionaryDataMap.get(entry.getKey()+"#"+entry.getValue()) ;
				if( obj != null ) {
					desc = obj.toString() ;
				}
				Map<String,Object> value = new HashMap<>() ;
				value.put("code", entry.getValue()) ;
				value.put("desc", desc) ;
				mapObject.put(entry.getKey(), value) ;
			}
		}
}
